package com.power.ding.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.ding.api.IDi_userdeptFeign;
import com.power.ding.model.Di_userdept;
import com.power.ding.model.pageInfo.Di_userdeptPageInfo;
import com.power.ding.service.Di_userdeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   用户部门关系控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Di_userdeptController",tags={"用户部门关系"})
public class Di_userdeptController implements IDi_userdeptFeign{

    @Resource
    private Di_userdeptService di_userdeptService;

    
    @Override
    @ApiOperation("查询用户部门关系分页列表")
    @SaCheckPermission("di_userdept_list")
    public ResponseDTO list(Di_userdeptPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Di_userdept> page = this.di_userdeptService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Di_userdept di_userdept : page) {
                di_userdept.geneText();
                list.add(di_userdept);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看用户部门关系")
    @SaCheckPermission("di_userdept_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Di_userdept di_userdept  = di_userdeptService.getById(id);
        if (di_userdept != null) {
            di_userdept.geneText();
        }
        return ResponseDTO.succData(di_userdept);
    }

    @Override
    @ApiOperation("删除用户部门关系")
    @SaCheckPermission("di_userdept_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        di_userdeptService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改用户部门关系")
    @SaCheckPermission("di_userdept_save")
    public ResponseDTO save(Di_userdept di_userdept) {
        Assert.notNull(di_userdept, "di_userdept不能为空");
        di_userdeptService.saveOrUpdate(di_userdept);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除用户部门关系")
    @SaCheckPermission("di_userdept_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        di_userdeptService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("di_userdept_list")
    public void output(HttpServletResponse response, Di_userdeptPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "用户部门关系";
        String[] heads = {"序号","删除标识","最后修改时间","用户id","部门id","机构id","是否主部门","是否主部门文本","显示顺序"};
        String[] fields = {"id","rowstate","lastupdatetime","userid","orgid","unitid","ismain","ismaintext","showorder"};
        Page<Di_userdept> page = this.di_userdeptService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "用户部门关系";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","删除标识","最后修改时间","用户id","部门id","机构id","是否主部门","是否主部门文本","显示顺序"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","rowstate","lastupdatetime","userid","orgid","unitid","ismain","ismaintext","showorder"};
        List<Di_userdept> list = (List<Di_userdept>)ExcelUtil.Import(fields, file, Di_userdept.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.di_userdeptService.saveAll(list);
        }
        return ResponseDTO.succ();
	}
    
}
